﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Store.Model.Models;
using Store.Bll;
using Store.ViewModel;
using System.IO;
using Microsoft.AspNetCore.Cors;

namespace Store.Server.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class CommunityController : ControllerBase
    {
        private readonly CommunityBll bll;
        public CommunityController(CommunityBll _bll)
        {
            bll = _bll;
        }

        /// <summary>
        /// 显示商品信息
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="state"></param>
        /// <param name="name"></param>
        /// <param name="brand"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(int sort=0, int state=0, string name="", string brand="", int page = 1, int limit = 20)
        {
            int total;
            var data = bll.GetCommodityList(sort, state,name,brand, page, limit, out total);
            return Ok(new { data = data, total = total });
        }



        /// <summary>
        /// 显示商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public List<Commodity> GetCommodity(int id)
        {
            return bll.GetCommodity(id);
        }

        /// <summary>
        /// 添加商品信息
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        [HttpPost]
        public int Add(Commodity c)
        {
            return bll.CommodityAdd(c);
        }

        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        [HttpPost]
        public int Edit(Commodity c)
        {
            return bll.UpdCommodity(c);
        }

        /// <summary>
        /// 修改商品状态
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        [HttpPost]
        public int EditState(Commodity c)
        {
            return bll.UpdComState(c);
        }


        /// <summary>
        /// 删除商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public int Del(int id)
        {
            return bll.CommodityDel(id);
        }


        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public int DelList(string[] ids)
        {
            return bll.CommodityDelList(ids);
        }
    }
}
